/**
 * @author Andrew Dashin <dashin@andrewdashin.com>
 */

var ie = (navigator.userAgent.toLowerCase().indexOf('msie') != -1);
var maxIndex = 1;
var dObj, tmpX, tmpY;

//var startX = 117;
//var startY = 18;

var Garbage = new Array();

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
function $(sId) {
    return document.getElementById(sId);
}

/*
 * This is Garbage Collector
 * It disposes all objects, that closed with dispose async
 */
function GarbageCollector() {
	var nLength = Garbage.length;
	if(nLength > 0)
		for(var i = 0; i < nLength; i++) {
			if(Garbage[i].style.display == 'none') {
				Garbage[i].dispose();
				Garbage.pop(i);
			}
		}
}

// Start garbage collector
setInterval(GarbageCollector, 1000);

/*
 * This is global function
 * And it is used from menu to generate sql code
 */
function generateSQL() {
	var sSQL = '';
	for (var i = 0; i < MyEntity.prototype.entities.length; i++) {
		if (MyEntity.prototype.entities[i] != null)
			sSQL += (sSQL != '' ? '<br /><br />': '') + MyEntity.prototype.entities[i].generateSQL();
	}
	var SQLText = new MySqlText();
	SQLText.setText(sSQL);
}

function getMaxIndex(nIndex) {
    if(nIndex < maxIndex)
        return maxIndex++;
    else
        return nIndex;
}

function getE(e) {
    if (document.all) 
        return window.event;
    else
        e.srcElement = e.target;
    return e;
}

function mouseDown(e) {
    e = getE(e);

    dObj = e.srcElement.getBar ? e.srcElement.getBar() : e.srcElement.parentNode;
	 
    if (!dObj) return false;

    var left = _parseInt(!window.getComputedStyle ? dObj.style['left']
        : dObj.style['left'] || window.getComputedStyle(dObj, null).getPropertyValue('left'));
    var top  = _parseInt(!window.getComputedStyle ? dObj.style['top']
        : dObj.style['top'] || window.getComputedStyle(dObj, null).getPropertyValue('top'));
        
    tmpX = _parseInt(left) - e.clientX;
    tmpY = _parseInt(top) - e.clientY;
    
	/*
	wireOnTop �������� �� StartDrag ��� ���-�� �����
	���� ���� �����-�� ��������-�����. ������ ��� ����� �� ���������
	����������������
	*/
	// probably this needs to have an additional check -
	// if dObj.getThis()
    if(dObj.getThis) {
        if(dObj.getThis().onTouch) {
            dObj.getThis().onTouch();
        }
        //dObj.style.zIndex = getMaxIndex(dObj.style.zIndex);
        dObj.getThis().onTop();
		
	  	//if (dObj.getThis().display)
		 //if (!dObj.getThis().display.style)
		   //dObj = false;
		   //return;
    }

    document.onmousemove    = mouseMove;
    document.onmouseup      = mouseUp;

    return false;
}

var Class = {
  create: function() {
    return function() {
      var oObj = this;
      this.div = document.createElement('div');
      this.div.getThis = function () {
      	return oObj;
      };
      /*
    if(this.base && this.base.length > 0) {
    	for(var i = 0; i < this.base.length; i++)
		   	this.base[i].apply(this, arguments);
    }
    */
   	this.initialize.apply(this, arguments);
    }
  }
}

Object.extend = function(destination, source) {
//	destination.base = new Array();
  for (var property in source) {
//  	if(property == 'initialize')
//  		destination['base'].push(destination['initialize']);
	destination[property] = source[property];
		
  }
  return destination;
}

function mouseMove(e) {
    if (!dObj) return false;
    e = getE(e);

	var newX = parseInt(tmpX + e.clientX);
	var newY = parseInt(tmpY + e.clientY);

	if(dObj.getThis)
		if(dObj.getThis().display && dObj.getThis().display.style) {
            // Temporary solution!!!
            
			var oPoint = new JTKPoint(dObj.getThis().display.style.left, dObj.getThis().display.style.top);
			if(newX < oPoint.x)
				newX = oPoint.x;
			if(newY < oPoint.y)
				newY = oPoint.y;

			var oPoint = new JTKPoint((dObj.getThis().display.style.left + dObj.getThis().display.offsetWidth), (dObj.getThis().display.style.top + dObj.getThis().display.offsetHeight));
			if(newX > (oPoint.x - dObj.offsetWidth))
				newX = oPoint.x - dObj.offsetWidth;
			if(newY > (oPoint.y - dObj.offsetHeight))
				newY = oPoint.y - dObj.offsetHeight;
		} else {return;}

	dObj.style.left = newX;
	dObj.style.top  = newY;

    if(dObj.getThis().redrawWires)
        dObj.getThis().redrawWires();
    return false;
}

function mouseUp(e) {
    if (!dObj) return false;

    document.onmousemove = null;
    document.onmouseup = null;
    dObj = null;
    return false;
}

if (!ie) {
    document.captureEvents(Event.MOUSEDOWN);
}

//document.onmousedown = mouseDown;

function _parseInt(value) {
    return isNaN(parseInt(value)) ? 0 : parseInt(value);
}

function JTKPoint(x, y) {
    this.x = x;
    this.y = y;
}

var JTKWidget = Class.create();
JTKWidget.prototype = {
	div: document.createElement('div'),
	display: document.body,
	initialize: function () {
		//this.div.style.zIndex = getMaxIndex(this.div.style.zIndex);
	},
	
    /**
    * @method
    * @description
    */ 
	onTop: function () {
		this.div.style.zIndex = getMaxIndex(this.div.style.zIndex);
	},
    
    /**
    * @param {String} sHTML this html to be set as div innerHTML
    * @description
    */
	setInnerHTML: function (sHTML) {
		this.div.innerHTML = sHTML;
	},
    /**
    * @method
    * @description
    */ 
	setClass: function (sClass) {
		this.div.className = sClass;
	},
	
    /**
    * @method
    * @description
    */ 
	setId: function (sId) {
		this.div.id = sId;
	},
	
    /**
    * @method
    * @param {Integer} nHeight
    * @description Sets height of the widget
    */ 
	setHeight: function (nHeight) {
		this.div.style.height = nHeight;
	},
	
    /**
    * @method
    * @description
    * @return Current height
    */ 
	getHeight: function () {
		return this.div.style.height;
	},
	
    /**
    * @method
    * @param {Integer} nWidth
    * @description
    */ 
	setWidth: function (nWidth) {
		this.div.style.width = nWidth;
	},
	
    /**
    * @method
    * @description
    * @return Current width
    */ 
	getWidth: function () {
		return this.div.style.width;
	},

    /**
    * @method
    * @description
    * @return Current width
    */ 	
	scrollBottom: function() {
		this.div.scrollTop = this.div.scrollHeight;
	},
    /**
    * @method
    * @param {JTKPoint} oPoint
    * @description Sets widget position
    */ 
	setPosition: function (oPoint) {
		this.div.style.left = oPoint.x;
		this.div.style.top  = oPoint.y;
	},
	
    /**
    * @method
    * @description
    * @return {JTKPoint} Returns current position
    */ 
	getPosition: function () {
		return new JTKPoint(this.div.style.left, this.div.style.top);
	},

	setVisibility: function (bVisible) {
		if (bVisible == !false) {
			this.div.style.display = "block";
		} else {
			this.div.style.display = "none";
		}
	},
	
	visible: function () {
		return this.div.style.display != "none";
	},
    /**
    * @method
    * @description
    */ 
	show: function (oDisplay) {
		if(typeof(oDisplay) == 'undefined') {
			this.display = document.body;
			var oDisplay = this.display;
			oDisplay.appendChild(this.div);
		} else {
			this.display = oDisplay;
            // This is not good, but anyway :)
            if (typeof(oDisplay.div) != 'undefined') {
                oDisplay.div.appendChild(this.div);
            } else {
                oDisplay.appendChild(this.div);
            }
		}
		this.div.style.zIndex = getMaxIndex(this.div.style.zIndex);
	},
	
    /**
    * @method
	* @desription
	*/ 
	getStyle: function (sStyle) {
		if(window.getComputedStyle)
			var Style = window.getComputedStyle(this.div,null).getPropertyValue(sStyle);
		else if(document.defaultView)
			var Style = document.defaultView.getComputedStyle(this.div,null).GetPropertyValue(sStyle);
		return Style
	},

    /**
    * @method
    * @description
    */ 
	getDisplayContext: function () {
		return this.display;
	},
	
    /**
    * @method
    * @description
    */ 
	setDisplayContext: function (oDisplay) {
		this.display = oDisplay;
	},
	
    /**
    * @method
    * @description
    */ 
	addWidget: function (oWidget) {
		oWidget.show(this);
	},
	
    /**
    * @method
    * @description
    */ 
	append: function (sName, oId) {
		if(!this.div.childItems)
			this.div.childItems = new Array();
		this.div.childItems[sName] = oId;
		this.div.appendChild(oId);
	},
	
    /**
    * @method
    * @description
    */ 
	dispose: function() {
		if (this.display != document.body && typeof(this.display.div) != 'undefined') 
			this.display.div.removeChild(this.div);
		else
			this.display.removeChild(this.div);
	},
	disposeAsync: function() {
		Effect.SwitchOff(this.div);
		Garbage.push(this.div);
	}
};

var JTKWidgetContainer = Class.create();
JTKWidgetContainer.prototype = Object.extend(new JTKWidget(), {
	initialize: function() {
		this.canLimitMovement = true;
	},
	
    /**
    * @method
    * @description
    */ 
	setMaxPoint: function(oPoint)
	{
		this.maxPoint = oPoint;
	},
	
    /**
    * @method
    * @description
    */ 
	setMinPoint: function(oPoint)
	{
		this.minPoint = oPoint;
	},
	
    /**
    * @method
    * @description
    */ 
	getMaxPoint: function()
	{
        this.maxPoint = new JTKPoint((this.div.style.left + this.div.offsetWidth), (this.div.style.top + this.div.offsetHeight));
		return this.maxPoint;
	},
	
    /**
    * @method
    * @description
    */ 
	getMinPoint: function()
	{
        this.minPoint = new JTKPoint(this.div.style.left, this.div.style.top);
		return this.minPoint;
	}
});

var JTKLock = Class.create();
JTKLock.prototype = Object.extend(new JTKWidget(), {
	initialize: function () {
		this.setClass('JTKLock');
        this.onTop();
	},
	
    /**
    * @method
    * @description
    */ 
	lockId: function (sId) {
		var oObj = document.getElementById(sId);
			if(oObj && !oObj.locked) {
				var oLock = new JTKLock();
					oObj.appendChild(oLock);
					oObj.locked = oLock;
			}
	},
	
    
    /**
    * @method
    * @description
    */
	lockClass: function (sClassName)
	{
		var aObj = document.getElementsByTagName('div');
		var nLength = aObj.length;
			for(var i = nLength - 1; i > 0; i--) {
				if(aObj[i].className == sClassName) {
					JTKLock.prototype.lockObject.call(this, aObj[i]);
				}
			}
	},
	
    /**
    * @method
    * @description
    */ 
	unLockClass: function (sClassName) {
		var aObj = document.getElementsByTagName('div');
		var nLength = aObj.length;
			for(var i = nLength - 1; i > 0; i--) {
				if(aObj[i].className == sClassName) {
					JTKLock.prototype.unLockObject.call(this, aObj[i]);
				}
			}
	},
	
    /**
    * @method
    * @description
    */ 
	lockObject: function (oObj) {
		if(!oObj.locked) {
			var oLock = new JTKLock();
				oObj.appendChild(oLock.div);
				oObj.locked = oLock;
		}
	},
   /**
    * @method
    * @description
    */ 
	unLockObject: function (oObj) {
		if(oObj.locked) {
			oObj.removeChild(oObj.locked.div);
			oObj.locked = false;
		}
	}
});

var JTKBar = Class.create();
JTKBar.prototype = Object.extend(new JTKWidget(), {
    title: "",
	initialize: function(sTitle) {
	    var sTitle = typeof(sTitle) != 'undefined' ? sTitle : '';
	    this.title = new JTKWidget();
	    this.title.div.innerHTML = sTitle;
	    this.title.show(this);
		var oThis = this;

		this.title.div.getBar = function () {
			return oThis.div.parentNode;
		};
	},
	setTitle: function (sTitle) {
	    this.title.div.innerHTML = sTitle;
	}
});

var JTKDraggableBar = Class.create();
JTKDraggableBar.prototype = Object.extend(new JTKBar(), {
	initialize: function (sName) {
		var oBar = new JTKBar();
		this._ = oBar.initialize;
		this._(sName);
//		this.base.apply(this, arguments);
//		JTKBar.apply(this, arguments);
//		JTKBar.initialize(sName);
		this.div.onmousedown = mouseDown;
		// вынести это в стиль
		this.div.style.cursor = 'move';
	}
});

var JTKImg = Class.create();
JTKImg.prototype = Object.extend(new JTKWidget(), {
	initialize: function () {
		oImg = document.createElement('img');
		this.div.appendChild(oImg);
		this.img = oImg;
	},
	
    /**
    * @method
    * @description
    */ 
	setImage: function (sSrc)
	{
		this.img.src = sSrc;
	},
	setOnClick: function (fOnClick) {
		this.img.onclick = fOnClick;
	}
});

var JTKWindow = Class.create();
JTKWindow.prototype = Object.extend(new JTKWidget(), {
	initialize: function () {
		//
	},
	
    /**
    * @method
    * @description
    */ 
	setId: function (sId) {
		this.div.id = sId;
	},
	
    /**
    * @method
    * @description
    */ 
	appendBar: function (oBar) {
		this.bar = oBar;
		this.div.appendChild(oBar.div);
	}
});

var JTKArrow = Class.create();
JTKArrow.prototype = Object.extend(new JTKImg(), {
    initialize: function(sSrc){
    	JTKImg.prototype.initialize.call(this);
		this.setImage(sSrc);
    },
    
    /**
    * @method
    * @description
    */ 
	redraw: function (oPoint, nWidth) {
		//this.setPosition(new JTKPoint(oPoint.x + nWidth - 15, oPoint.y - 4));
		this.setPosition(new JTKPoint(oPoint.x + nWidth - this.img.width, oPoint.y - 3));
	},
	
    /**
    * @method
    * @description
    */ 
	onTop: function() {
		this.div.style.zIndex = getMaxIndex(this.div.style.zIndex);
	}
});

var JTKWire = Class.create();
JTKWire.prototype = Object.extend(new JTKWidget(), {
	initialize: function (oPoint) {
		if(typeof(oPoint) != 'undefined')
			this.setPosition(oPoint);
	},
	
    /**
    * @method
    * @description
    */ 
	onTop: function () 
	{
		this.div.style.zIndex = getMaxIndex(this.div.style.zIndex);
	},
	next: null,
	prev: null
});

var JTKArrowedWire = Class.create();
JTKArrowedWire.prototype = Object.extend(new JTKWire(), {
	initialize: function (oPoint) {
		/*
		var oObj = new JTKWire(oPoint);
			this.oldOnTop = oObj.onTop;
		*/
		this.oldOnTop = JTKWire.prototype.onTop;
	},
	
    /**
    * @method
    * @description
    */ 
	setArrow: function (oArrow)
	{
		this.arrow = oArrow;
//			this.arrow.setDisplayContext(this.display);
		this.arrow.show(this.display);
	},
	
    /**
    * @method
    * @description
    */ 
	onTop: function ()
	{
		this.oldOnTop();
		if(this.arrow) {
			this.arrow.onTop();
		}			
	}
		
});

var JTKHorizontalArrowedWire = Class.create();
JTKHorizontalArrowedWire.prototype = Object.extend(new JTKArrowedWire(), {
	initialize: function (oHeadPoint, nWidth) {
		JTKArrowedWire.prototype.initialize.call(this, oHeadPoint);
		this.setClass('horizontalWire');
		this.setWidth(nWidth);
	},
	
    /**
    * @method
    * @description
    */ 
	redraw: function (oPoint, nWidth)
	{
		this.setPosition(oPoint);
		this.setWidth(nWidth);
		if(this.arrow) {
			// ������� ����� �������
			this.arrow.redraw(oPoint, nWidth);
		}
		//this.style.zIndex = getMaxIndex(this.style.zIndex);
	}
});

var JTKVerticalArrowedWire = Class.create();
JTKVerticalArrowedWire.prototype = Object.extend(new JTKArrowedWire(), {
	initialize: function (oHeadPoint, nHeight) {
		//
	    JTKArrowedWire.prototype.initialize.call(this, oHeadPoint);
		this.setClass('verticalWire');
		this.setHeight(nHeight);
	},
	
    /**
    * @method
    * @description
    */ 
	redraw: function (oPoint, nHeight) {
			this.setPosition(oPoint);
			// (nHeight == 0 ? 1 : nHeight) - that is the fix for IE
			// IE doesn't like divs with zero height
			this.setHeight(nHeight == 0 ? 1 : nHeight);
	}
});

var JTKConnectionWire = Class.create();
JTKConnectionWire.prototype = {
	initialize: function (oSrcSlot, oDstSlot, sWireName, oDisplay) {
	    this.id = sWireName;
	    this.oSrc = oSrcSlot;
	    this.oDst = oDstSlot;
	    this.oSrc.wire.push(this);
	    this.oDst.wire.push(this);
	    
	    this.redraw();

		this.src.show(oDisplay);
		this.vert.show(oDisplay);
		this.dst.show(oDisplay);

	//    this.redraw();
	},
	
    /**
    * @method
    * @description
    */ 
    redraw: function () {
        var oSrcSrcPoint = this.oSrc.getFieldOutPoint();
//        var oSrcDstPoint = this.oSrc.getFieldInPoint();
    
//        var oDstSrcPoint = this.oDst.getFieldOutPoint();
        var oDstDstPoint = this.oDst.getFieldInPoint();

//var menu = document.getElementById('menuBar');
//menu.innerHTML = "src.x: " + oSrcSrcPoint.x + " srcy: " + oSrcSrcPoint.y +
//" dstx: " + oDstDstPoint.x + " dsty: " + oDstDstPoint.y;

        var nSrcTailWidth;
        var oSrcTailPoint;
        var oVertPoint;
        var oDstTailPoint;
        var nDstTailWidth;
        var nVertHeigth;
        // 10 - ����� ���� ��������� ������ :)
        var difference = (oDstDstPoint.x - 15) -
                         (oSrcSrcPoint.x + 20);
        if(oSrcSrcPoint.x < oDstDstPoint.x && difference > 0) {
            var oWireSrcPoint = this.oSrc.getFieldOutPoint();
            var oWireDstPoint = this.oDst.getFieldInPoint();
            nSrcTailWidth = 20;
            oSrcTailPoint = new JTKPoint(oWireSrcPoint.x + 2, oWireSrcPoint.y);
            oDstTailPoint = new JTKPoint(oSrcTailPoint.x + nSrcTailWidth, oWireDstPoint.y);
            nDstTailWidth = oDstDstPoint.x - (oSrcTailPoint.x + nSrcTailWidth);
            // 2 - ������ �������
            nVertHeigth = Math.abs(oDstDstPoint.y - oSrcSrcPoint.y);
            
            if(oSrcSrcPoint.y < oDstDstPoint.y) {
                oVertPoint = new JTKPoint(oSrcTailPoint.x + nSrcTailWidth, oSrcTailPoint.y);
            } else {
                oVertPoint = new JTKPoint(oSrcTailPoint.x + nSrcTailWidth, oDstDstPoint.y);
            }
        } else {
            var oWireSrcPoint = this.oSrc.getFieldInPoint();
            var oWireDstPoint = this.oDst.getFieldInPoint();
            if(oWireSrcPoint.x < oWireDstPoint.x) {
                nSrcTailWidth = 5 + 15;
                oSrcTailPoint = new JTKPoint(oWireSrcPoint.x - nSrcTailWidth, oWireSrcPoint.y);
                nDstTailWidth = Math.abs(oSrcTailPoint.x - oWireDstPoint.x);
                oDstTailPoint = new JTKPoint(oSrcTailPoint.x, oWireDstPoint.y);
                nVertHeigth = Math.abs(oDstDstPoint.y - oSrcSrcPoint.y);
            } else {
                nDstTailWidth = 20;
                oDstTailPoint = new JTKPoint(oWireDstPoint.x - nDstTailWidth, oWireDstPoint.y);
                nSrcTailWidth = oWireSrcPoint.x - oDstTailPoint.x;
                oSrcTailPoint = new JTKPoint(oDstTailPoint.x, oWireSrcPoint.y);
                nVertHeigth = Math.abs(oDstDstPoint.y - oSrcSrcPoint.y);
            }
            
            if(oSrcSrcPoint.y < oDstDstPoint.y) {
                oVertPoint = new JTKPoint(oSrcTailPoint.x, oSrcTailPoint.y);
            } else {
                oVertPoint = new JTKPoint(oSrcTailPoint.x, oDstDstPoint.y);
            }
            
        }
		
        if(!this.vert) {
            this.vert = new JTKVerticalArrowedWire(oVertPoint, nVertHeigth);
            this.dst  = new JTKHorizontalArrowedWire(oDstTailPoint, nDstTailWidth);
            this.src  = new JTKHorizontalArrowedWire(oSrcTailPoint, nSrcTailWidth);
        } else {
            this.vert.redraw(oVertPoint, nVertHeigth);
            this.dst.redraw(oDstTailPoint, nDstTailWidth);
            this.src.redraw(oSrcTailPoint, nSrcTailWidth);
        }
    },
    
    /**
    * @method
    * @description
    */ 
    onTop: function () {
            this.vert.onTop();
            this.dst.onTop();
            this.src.onTop();
    },
    
    /**
    * @method
    * @description
    */ 
    dispose: function () {
		/*
		���� ������� ������ �� ��������� � ������� �� ������������
		� �� �� document.body
		*/
        document.body.removeChild(this.src.div);
        document.body.removeChild(this.vert.div);
        if(this.dst.arrow)
            document.body.removeChild(this.dst.arrow);
        document.body.removeChild(this.dst.div);
        //delete this;
		/*
		this.oSrc.wire.pop(this.src);
		this.oDst.wire.pop(this.dst);
		*/
		// oSrc � oDst - ��������-�����
		this.oSrc.disconnect();
		this.oDst.disconnect();
    },
    
    /**
    * @method
    * @description
    */ 
	getDstWire: function () {
		return this.dst;
	},
	
    /**
    * @method
    * @description
    */ 
	getSrcWire: function () {
		return this.src;
	}
};


var JTKConnectionSlot = Class.create();
JTKConnectionSlot.prototype = Object.extend(new JTKWidget(), {
	wire: new Array(),
	initialize: function () {
		
	},
	
    /**
    * @method
    * @description
    */ 
	getFieldOutPoint: function () {
		throw("Abstract method!(Class JTKConnectionSlot)");
	},
	
    /**
    * @method
    * @description
    */ 
	getFieldInPoint: function () {
		throw("Abstract method!(Class JTKConnectionSlot)");
	},
	
    /**
    * @method
    * @description
    */ 
	disconnectWire: function (oWire) {
		oWire.disconnect();
		this.wire.pop(oWire);
	},
	
    /**
    * @method
    * @description
    */ 
	disconnectWireIndex: function (i) {
		if (i < this.wire.length) {
			this.wire[i].disconnect();
			this.wire.pop(this.wire[i]);
		}
	},
	
    /**
    * @method
    * @description
    */ 
	redrawWires: function () {
		var nLength = this.wire.length;
		if(nLength > 0) {
			for(var i = 0; i < nLength; ++i) {
				this.wire[i].redraw();
			}
		}
	},
	
    /**
    * @method
    * @description
    */ 
	wireOnTop: function () {
		var nLength = this.wire.length;
		if(nLength > 0) {
			for(var i = 0; i < nLength; i++) {
				this.wire[i].onTop();
			}
		}
	}
});

var JTKConnectionBank = Class.create();
JTKConnectionBank.prototype = {
	slots: new Array(),
	initialize: function () {
		;
	},
	
    /**
    * @method
    * @description
    */ 
	addSlot: function (oSlot) {
		this.slots.push(oSlot);
	},
	
    /**
    * @method
    * @description
    */ 
	onTouch: function () {
		var nLength = this.slots.length;
		if(nLength > 0) {
			for(var i = 0; i < nLength; i++) {
				this.slots[i].wireOnTop();
			}
		}
	},
	
    /**
    * @method
    * @description
    */ 
	redraw: function () {
		var nLength = this.slots.length;
		if(nLength > 0) {
			for(var i = 0; i < nLength; ++i) {
				this.slots[i].redrawWires();
			}
		}
	}
};

var JTKWireableWindow = Class.create();
JTKWireableWindow.prototype = Object.extend(new JTKWindow(), {
	initialize: function () {
		this.connectionBank = new JTKConnectionBank();
	},
	
    /**
    * @method
    * @description
    */ 
	onTouch: function () {
		this.connectionBank.onTouch();
	},	
	
    /**
    * @method
    * @description
    */ 
	redrawWires: function () {
		this.connectionBank.redraw();
	}
});

var JTKInput = Class.create();
JTKInput.prototype = Object.extend(new JTKWidget(), {
	initialize: function () {
		this.input = document.createElement('input');
	},
	
    /**
    * @method setType
    * @description Sets the type of current input
    *               yes, this seems to be tricky, but IE
    *               can not change input type after it is already appended
    */ 
	setType: function (sType) {
	    this.input.type = sType;
	    this.div.appendChild(this.input);
	},
	getValue: function () {
	    return this.input.value;
	}
});

var JTKTextInput = Class.create();
JTKTextInput.prototype = Object.extend(new JTKInput(), {
	initialize: function () {
		JTKInput.prototype.initialize.call(this);
		this.setType('text');
	}
});

var JTKPasswordInput = Class.create();
JTKPasswordInput.prototype = Object.extend(new JTKInput(), {
	initialize: function () {
		JTKInput.prototype.initialize.call(this);
		this.setType('password');
	}
});

var JTKButtonInput = Class.create();
JTKButtonInput.prototype = Object.extend(new JTKInput(), {
	initialize: function (sCaption) {
		JTKInput.prototype.initialize.call(this);
		this.setType('button');
		this.input.value = sCaption;
	}
});

var JTKSelectBox = Class.create();
JTKSelectBox.prototype = Object.extend(new JTKWidget(), {
    initialize: function () {
        var oBox = document.createElement('select');
        this.select = oBox;
            this.div.appendChild(oBox);
    },
    addOption: function (sOption, sValue) {
        var oOption = document.createElement('option');
        // Safari's "options" object has not "add" methods
		// So we can't do this - this.select.options.add(oOption, 0);
		this.select.options[sValue] = oOption;
        oOption.value = sValue;
        // for IE
        oOption.innerText = sOption;
        // fot others
        oOption.text = sOption;
    },
    getSelectedIndex: function () {
        return this.select.selectedIndex;
    },
    loadArray: function (aValuesArray) {
        for (var i = aValuesArray.length - 1; i >= 0; i--) {
            this.addOption(aValuesArray[i], i);
        }
    }
});

var JTKLi = function () {
    var oObj = document.createElement('li');
    return oObj;
};

JTKLi.prototype.setClass = function(sClass) {
	this.className = sClass;
}
